﻿using System;
using System.Linq;
using System.Collections.Generic;

using Spencen.Mobile.Markup;
using Spencen.Mobile.UI;
using Spencen.Mobile.UI.Commanding;
using Spencen.Mobile.UI.Controls;
using Spencen.Mobile.UI.Navigation;
using Spencen.Mobile.UI.Templating;

using Commute.Model;

namespace Commute.Presenters
{
    public class ResultPresenter
    {
        private IDrawingHost _host;
        private INavigator _navigator;

        public ResultPresenter( INavigator navigator )
        {
            BackCommand = new DelegateCommand( Back );
            _navigator = navigator;
        }

        public ICommand BackCommand { get; private set; }
        public string RouteName { get; private set; }
        public ObservableList RouteResults { get; private set; }

        public View CreateView( IDrawingHost host )
        {
            _host = host;

            // XAML Loading
            var text = (System.IO.Stream)System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream( "Commute.Views.ResultView.xml" );
            var xamlRenderer = new XamlRenderer( host, this );
            return (View)xamlRenderer.Render( text );
        }

        public void Initialize( Timetable timetable, string routeName )
        {
            RouteName = routeName;

            var results = from service in timetable.Services
                          from route in service.Routes
                          where route.Name == routeName
                          from run in route.Runs
                          orderby run.Stops.First().Time
                          select new
                          {
                              Service = service,
                              Route = route,
                              Run = run
                          };

            RouteResults = new ObservableList( results );

            //var stops = new List<Stop>();
            //var upcomingIndex = 0;
            //var nextRunIndex = -1;
            //foreach (var result in results)
            //{
            //    string[] items = new string[4];
            //    items[0] = result.Service.Name;
            //    foreach (var runStop in result.Run.Stops)
            //    {
            //        var index = stops.IndexOf(runStop.Stop);
            //        if (index >= 0)
            //        {
            //            items[index + 1] = runStop.Time.ToString(DefaultTimeFormat);
            //        }
            //        else if (stops.Count < 3)
            //        {
            //            stops.Add(runStop.Stop);
            //            index = stops.Count;
            //            items[index] = runStop.Time.ToString(DefaultTimeFormat);
            //            listView1.Columns[index].Text = runStop.Stop.Name;
            //        }
            //        else
            //            continue; // Only supports 3 stops at this point in time.
            //    }
        }

        private void Back()
        {
            _navigator.Back();
        }
    }
}
